<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "auth_order".
 *
 * @property int $id
 * @property int $auth_app_id
 * @property int $auth_shop_id
 * @property string|null $order_no
 * @property int $pay_fee 买家支付金额
 * @property int $status 订单状态
 */
class AuthOrder extends \yii\db\ActiveRecord
{
    //订单状态
    const STATUS_WAIT_POST = 1;
    const STATUS_WAIT_SIGN = 2;
    const STATUS_SIGNED = 3;
    const STATUS_UNKNOW = 4;
    //售后状态
    const REFUND_NULL = 1;
    const REFUND_YES = 2;
    const REFUND_UNKNOW = 3;
    static public $state_arr = [
        self::STATUS_WAIT_POST =>'待发货',
        self::STATUS_WAIT_SIGN =>'待签收',
        self::STATUS_SIGNED =>'已签收',
        self::STATUS_UNKNOW =>'未知',
    ];
    static public $refund_arr = [
        self::REFUND_NULL =>'无售后',
        self::REFUND_YES =>'有售后',
    ];
    //获取返现说明
    public function getCashbackIntro(){
        $uid = $this->authShop->uid;
        $qr = BuyerQr::find()->andWhere(['type'=>BuyerQr::TYPE_USER,'type_id'=>$uid])->limit(1)->one();
        if(empty($qr)) return '';
        return $qr->content;
    }
    //订单状态
    public function showState(){
        return self::$state_arr[$this->status]??self::$state_arr[self::STATE_UNKNOW];
    }
    //售后状态
    public function showRefund(){
        return self::$refund_arr[$this->refund_status]??self::$refund_arr[self::REFUND_UNKNOW];
    }
    //关联
    public function getAuthApp(){
        return $this->hasOne(AuthApp::className(),['id'=>'auth_app_id']);
    }
    //关联
    public function getAuthShop(){
        return $this->hasOne(AuthShop::className(),['id'=>'auth_shop_id']);
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'auth_order';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['auth_app_id', 'auth_shop_id'], 'required'],
            [['auth_app_id', 'auth_shop_id', 'pay_fee', 'status'], 'integer'],
            [['order_no'], 'string', 'max' => 255],
            [['auth_shop_id', 'order_no'], 'unique', 'targetAttribute' => ['auth_shop_id', 'order_no']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'auth_app_id' => '平台',
            'auth_shop_id' => '店铺',
            'order_no' => '订单号',
            'pay_fee' => '支付金额',
            'status' => '状态',
        ];
    }
}
